﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Odbc;
using BloomFilter.String;
using System.Text.RegularExpressions;
using BloomFilter;
using BZ.Common.FileUtility;

namespace BloomFilterBuilder
{


    class Program
    {
        static OdbcConnection _odbcCon;

        static void Main(string[] args)
        {
            Console.WriteLine("      *****程序启动*****");

            string _msg = string.Empty;
            bool rslt = InitConnection(out _msg);

            List<string> urls = new List<string>();
            Console.WriteLine("打开数据库连接：" + _msg);


            //*****************************数据获取****************************************
            if (rslt)
            {
                Console.WriteLine("      *****获取数据*****");
                using (OdbcCommand _cmd = new OdbcCommand())
                {
                    _cmd.CommandText = "SELECT count(*) FROM app_collection";
                    _cmd.Connection = _odbcCon;

                    try
                    {
                        int rowCount = Convert.ToInt32(_cmd.ExecuteScalar());
                        Console.WriteLine("表中包含数据(行)：" + rowCount);

                        _cmd.CommandText = "SELECT extra1 FROM app_collection;";
                        OdbcDataReader _reader = _cmd.ExecuteReader();
                        while (_reader.Read())
                        {
                            urls.Add(_reader[0].ToString());
                        }


                        Console.WriteLine("      ***数据加载完成***");
                        Console.WriteLine("加载行数：" + urls.Count);
                        rslt = true;

                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("数据库错误：" + ex.Message);
                        rslt = false;
                    }
                }
            }

            //*****************************数据处理****************************************
            if (rslt)
            {
                Console.WriteLine("  *****开始生成过滤文件*****");
                StringBloomFilterManager myManager = new StringBloomFilterManager(1024 * 1024 * 8, 3, @"E:\filtertest");
                if (!myManager.Init(out _msg, false))
                {
                    Console.WriteLine("过滤管理器初始化错误：" + _msg);
                }
                else
                {
                    Console.WriteLine("开始过滤。");
                }

                int totalCount = 0;
                int addCount = 0;
                int crashCount = 0;

                TextFileHelper log = TextFileHelper.CreateTextFileHelper(@"e:\filterlog.txt", out _msg, System.IO.FileAccess.Write, Encoding.Unicode, true);
                log.OpenFile();

                foreach (string myurl in urls)
                {
                    if (string.IsNullOrEmpty(myurl))
                    {
                        continue;
                    }

                    totalCount += 1;

                    if (!myManager.Add(new UrlFilterParam(myurl)))
                    {
                        crashCount += 1;
                        log.Write(myurl, true);
                    }
                    else
                    {
                        addCount += 1;
                    }
                }
                log.CloseFile();
                myManager.SaveFilters();
                Console.WriteLine("过滤完成：总共[{0}]条，增加数据[{1}]条，冲突数据[{2}]。", totalCount, addCount, crashCount);
            }

            Console.ReadKey();
        }

        static bool InitConnection(out string proMsg)
        {
            bool rslt = false;
            proMsg = string.Empty;
            try
            {
                _odbcCon = new OdbcConnection("DSN=PostgreSQLDB");
                _odbcCon.Open();
                rslt = true;
                proMsg = "数据库连接成功";
            }
            catch (Exception ex)
            {
                proMsg = ex.Message;
            }
            return rslt;
        }
    }
}
